// 数据库测试脚本
import DatabaseService from './database';
import LotteryApiService from './lotteryApi';

// 测试数据库功能
export const testDatabase = async () => {
  console.log('=== 开始数据库测试 ===');
  
  try {
    // 1. 测试获取记录数量
    const count = await DatabaseService.getLotteryHistoryCount();
    console.log('1. 当前记录数量:', count);
    
    // 2. 测试获取所有记录
    const allRecords = await DatabaseService.getAllLotteryHistory();
    console.log('2. 所有记录:', allRecords);
    
    // 3. 测试模拟数据
    const mockData = LotteryApiService.getMockData();
    console.log('3. 模拟数据:', mockData);
    
    // 4. 测试添加一条记录
    if (mockData.length > 0) {
      const testRecord = mockData[0];
      console.log('4. 尝试添加测试记录:', testRecord);
      
      const result = await DatabaseService.addLotteryHistory(testRecord);
      console.log('4. 添加结果:', result);
      
      // 5. 再次检查记录数量
      const newCount = await DatabaseService.getLotteryHistoryCount();
      console.log('5. 添加后记录数量:', newCount);
      
      // 6. 再次获取所有记录
      const newAllRecords = await DatabaseService.getAllLotteryHistory();
      console.log('6. 添加后所有记录:', newAllRecords);
    }
    
    console.log('=== 数据库测试完成 ===');
    return {
      success: true,
      count: await DatabaseService.getLotteryHistoryCount(),
      records: await DatabaseService.getAllLotteryHistory()
    };
    
  } catch (error) {
    console.error('数据库测试失败:', error);
    return {
      success: false,
      error: error.message
    };
  }
};

// 测试API服务
export const testApiService = () => {
  console.log('=== 开始API服务测试 ===');
  
  try {
    // 1. 检查API密钥配置
    const isConfigured = LotteryApiService.isApiKeyConfigured();
    console.log('1. API密钥是否配置:', isConfigured);
    
    // 2. 获取模拟数据
    const mockData = LotteryApiService.getMockData();
    console.log('2. 模拟数据数量:', mockData.length);
    console.log('2. 模拟数据示例:', mockData[0]);
    
    console.log('=== API服务测试完成 ===');
    return {
      success: true,
      isConfigured,
      mockDataCount: mockData.length
    };
    
  } catch (error) {
    console.error('API服务测试失败:', error);
    return {
      success: false,
      error: error.message
    };
  }
};

// 综合测试
export const runAllTests = async () => {
  console.log('=== 开始综合测试 ===');
  
  const apiTest = testApiService();
  const dbTest = await testDatabase();
  
  console.log('=== 综合测试结果 ===');
  console.log('API测试:', apiTest);
  console.log('数据库测试:', dbTest);
  
  return {
    api: apiTest,
    database: dbTest
  };
};